package com.taxshare.seller.service;

import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.taxshare.seller.constant.CodeConstant;
import com.taxshare.seller.entity.BlankInvoiceCollctionEntity;
import com.taxshare.seller.remote.ApiClientService;
import com.taxshare.seller.util.InvoiceHelper;
import com.taxshare.seller.util.sst.IntJsonUtil;
import com.taxshare.seller.view.dto.BlankInvoiceCollctionDTO;
import com.taxshare.seller.view.dto.SellerInvoiceDTO;
import com.taxshare.tools.util.UUIDUitl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * @author YANG
 * @date 2021/3/19
 */
@Slf4j
@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
public class SellerCommonService {

    @Autowired
    BlankInvoiceCollctionService blankInvoiceCollctionService;
    
    @Autowired
    ApiClientService apiClientService;

    /**
     * 更新领购发票
     */
    public void updateCollectionInvoice(Map map) {
        // 根据盘号、发票类型,发票代码、发票起号、领购日期查询库中是否已存在
        String invoiceType = InvoiceHelper.ptransFplxdm(MapUtil.getStr(map, "FPZL_DM"));
        String invoiceCode = MapUtil.getStr(map, "DQFPDM");
        QueryWrapper<BlankInvoiceCollctionEntity> queryWrapper = new QueryWrapper();
        queryWrapper.eq("invoice_type", invoiceType);
        queryWrapper.eq("invoice_code", invoiceCode);
        List<BlankInvoiceCollctionEntity> list = blankInvoiceCollctionService.list(queryWrapper);
        if (list!=null && list.size() > 0) {
            BlankInvoiceCollctionEntity blankInvoiceCollctionEntity = blankInvoiceCollctionService.getOne(queryWrapper);
            blankInvoiceCollctionEntity.setInvoiceStartNo(MapUtil.getStr(map, "DQFPHM"));
            blankInvoiceCollctionEntity.setInvoiceSheet(MapUtil.getStr(map, "SYFS"));
            blankInvoiceCollctionService.update(blankInvoiceCollctionEntity, queryWrapper);
        } else {
            BlankInvoiceCollctionEntity blankInvoiceCollctionEntity = new BlankInvoiceCollctionEntity();
            blankInvoiceCollctionEntity.setUuid(UUIDUitl.UUID32(false));
            blankInvoiceCollctionEntity.setInvoiceType(invoiceType);
            blankInvoiceCollctionEntity.setInvoiceCode(invoiceCode);
            blankInvoiceCollctionEntity.setInvoiceStartNo(MapUtils.getString(map, "DQFPHM"));
            blankInvoiceCollctionEntity.setInvoiceSheet(MapUtils.getString(map, "SYFS"));
            blankInvoiceCollctionService.save(blankInvoiceCollctionEntity);
        }
    }

}
